<?php
/**
 * CpController
 * 
 * @author
 * @version 
 */
require_once 'Zend/Controller/Action.php';
class Admin_DuAnController extends Sunnet_Controller_Action
{
	var $_arrParam;
	public function init()
    {
//    	Truyền các tham số nhận được ra view
    	$this->_arrParam = $this->_request->getParams();
    	$this->view->arrParam = $this->_arrParam;
    	if (empty($this->_arrParam['page'])) {
    		$this->_arrParam['page'] = 1;
    	}
    	if (empty($this->_arrParam['line_per_page'])) {
    		$this->_arrParam['line_per_page'] = 10;
    	}
        Zend_Loader::loadClass('Admin_Models_MenuModel');
         $db = new Admin_Models_MenuModel();
         $mainmenu = $db->loadmainmenu();
         $this->view->mainmenu = $mainmenu; 
         
         function vn_str_filter ($str){
			$unicode = array(
					'a'=>'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ',
					'd'=>'đ',
					'e'=>'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ',
					'i'=>'í|ì|ỉ|ĩ|ị',
					'o'=>'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ',
					'u'=>'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự',
					'y'=>'ý|ỳ|ỷ|ỹ|ỵ',
					'A'=>'Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
					'D'=>'Đ',
					'E'=>'É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
					'I'=>'Í|Ì|Ỉ|Ĩ|Ị',
					'O'=>'Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
					'U'=>'Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
					'Y'=>'Ý|Ỳ|Ỷ|Ỹ|Ỵ',
					'_'=>' ',
			);
		
			foreach($unicode as $nonUnicode=>$uni){
				$str = preg_replace("/($uni)/i", $nonUnicode, $str);
			}
			return $str;
		}
    }

    public function preDispatch(){
        $this->view->headScript()->appendFile('/js/ckeditor/ckeditor.js','text/javascript');
         $template_path = TEMPLATE_PATH . "/admin/default/";
		$this->loadTemplate($template_path,'template.ini','template');           
    	$auth = Zend_Auth::getInstance();
        $this->view->userName = $auth->getIdentity()->username;
    	if ((!$auth->hasIdentity() || !$auth->getIdentity()->admin) && ($this->_arrParam['action'] != 'login')) {
    		$this->_forward('login','index','admin',array('action'	=> 'login'));//,$this->_arrParam);
    	}
        $this->view->headScript()->appendFile('/js/ckeditor/ckeditor.js','text/javascript');
        $this->view->headScript()->appendFile('/js/ckeditor/styles.js','text/javascript');
        $this->view->headScript()->appendFile('/js/ckeditor/ckeditor.css','text/css');
    }

    public function indexAction(){
         Zend_loader::loadClass('Admin_Models_DuAnModel');
//		Load các model và form sử dụng trong action
         $search = $this->_request->getParam('search');
         $field  = $this->_request->getParam('field');
         $group_id  = $this->_request->getParam('group_field');
         
         $db_list = new Admin_Models_DuAnModel();
         $list = $db_list->listduan();

         $this->view->list = $list;

    }

    public function addAction(){
        Zend_loader::loadClass('Admin_Forms_AddDuAnForm');
        Zend_loader::loadClass('Admin_Models_DuAnModel');

        $session = new Zend_Session_Namespace();

        $form = new Admin_Forms_AddDuAnForm(null);
        //Zend_debug::dump($form);
      	$this->view->form = $form;

      	$request=$this->getRequest();
				
		if($request->isPost()){
	

				try {
                                        $db= new Admin_Models_DuAnModel();
										
										$data= array(
												'nameq'=>  $this->_request->getParam('nameq'),
										);

										try{
					                        $result = $db->add($data);

										}catch(Exception $e)
										{
											echo $e;
										}

									if($result>0)
									{
										// /alert('Note added');
										echo "<script>window.location = '/admin/du-an/';</script>";
										
									}else {
										echo "<script>alert('Add Fail');window.location = '/admin/du-an/';</script>";
									}

				} catch (Exception $e) {
					echo $e;
				}
					
		}
    }
    

    public function editAction(){

         $id = $this->_request->getParam('id');

        if(intval($id)>0){ 

	        Zend_loader::loadClass('Admin_Forms_AddDuAnForm');
	        Zend_loader::loadClass('Admin_Models_DuAnModel');

	        $form = new Admin_Forms_AddDuAnForm(null);

			$db = new Admin_Models_DuAnModel();
	        $result_edit = $db->loadbyid($id);

	        $form->setDefaults($result_edit); 
			        
	        $this->view->form=$form;
			$this->view->data = $result_edit;

	        $request=$this->getRequest();
	        if($request->isPost()){
				try {
										$data= array(
												'nameq'=>  $this->_request->getParam('nameq'),
										);

										try{
					                        $result = $db->edit($data, $id);

										}catch(Exception $e)
										{
											echo $e;
										}

										if($result>0)
										{
											// /alert('Note added');
											echo "<script>window.location = '/admin/du-an/';</script>";
											
										}else {
											echo "<script>alert('Add Fail');window.location = '/admin/du-an/';</script>";
										}

				} catch (Exception $e) {
					echo $e;
				}
			} //end post edit
		} // neu ton tai id
    }


    
    public function del(){

		Zend_Loader::loadClass('Admin_Models_DuAnModel');
             $model = new Admin_Models_DuAnModel();
             $id = $this->_request->getParam('id');
             $result = $model->delQuanHuyen($id);
             
             $this->_redirect('/admin/du-an');
    }
   
}
